package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"golang.org/x/crypto/bcrypt"
	"mall/app/member/cmd/rpc/internal/svc"
	"mall/app/member/cmd/rpc/pb"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// Login 登录
func (l *LoginLogic) Login(in *pb.LoginReq) (*pb.LoginResp, error) {
	pwdFromDb, err := l.svcCtx.MemberModel.FindOneByPhone(l.ctx, in.Phone)
	if err != nil {
		l.Error(err)
		return nil, err
	}
	if len(pwdFromDb) == 0 {
		return nil, err
	}
	err = bcrypt.CompareHashAndPassword([]byte(pwdFromDb), []byte(in.Password))
	if err != nil {
		return nil, err
	}
	return &pb.LoginResp{}, nil
}
